// pages/activity/evaluationResult/index.js
const api = require("../../../config/api")
const request = require("../../../utils/request")
const { getStorage } = require("../../../utils/util")
const app = getApp()
let isNavigate = 0
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		topBack:'https://kj-zhiliao.oss-cn-qingdao.aliyuncs.com/zhiliao-applet/xgAzwTmqVY8ce5d3e4e2c5de4232126fe9532322a7f2.png',
		info:'https://kj-zhiliao.oss-cn-qingdao.aliyuncs.com/zhiliao-applet/UODuf3jz00fl7748fdc1e2081e8e2ad3dc4093779801.png',
		showTip:false
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		this.getResult()
	},

	getResult: async function(){
		let patientInfo = wx.getStorageSync('patientInfo')
		const res = await request.get(api.getEvaluationResult,{},{loading: false})
		console.log('监听页面加载',res);
		const {patientRecord,resultRecord} = res.data
		patientRecord.childHighNames = patientRecord.childHighNames.split(',')
		patientRecord.childLowName = patientRecord.childLowName.split(',')

		if(patientRecord.childHighNames){
			patientRecord.childHighNames.pop()
		}
		if(patientRecord.childLowName){
			patientRecord.childLowName.pop()
		}
		this.setData({
			patientInfo,
			patientRecord,
			resultRecord,
		})
	},

	// 孩子评估
	clickChildren:function(){
    isNavigate = 1
		wx.redirectTo({
			url: '/pages/activity/evaluationDescription/index',
		})
	},

	// 家长评估
	clickParent:function(){
    //判断是否是当前缓存--是否是家长缓存
    let patientId = wx.getStorageSync('patientInfo')?.id
    let userId = wx.getStorageSync('userId')
    let evaluationUserId = getStorage("evaluationUserId")
    let evaluationPatientId = getStorage("evaluationPatientId")
    let evaluationAnswerList = getStorage("evaluationAnswerList")
    let evaluationIsChild = getStorage("evaluationIsChild")

    if( evaluationAnswerList.length>0 && userId === evaluationUserId && patientId === evaluationPatientId && evaluationIsChild === 2){
      this.setData({
        showTip: true
      })
    }else{
      isNavigate = 1
      wx.redirectTo({
        url: '/pages/activity/evaluationAnswer/index?isChild=2',
      })
    }
	},
	// 家长重新评估
	clickParent2:function(){
		
    //判断是否是当前缓存--是否是家长缓存
    let patientId = wx.getStorageSync('patientInfo')?.id
    let userId = wx.getStorageSync('userId')
    let evaluationUserId = getStorage("evaluationUserId")
    let evaluationPatientId = getStorage("evaluationPatientId")
    let evaluationAnswerList = getStorage("evaluationAnswerList")
    let evaluationIsChild = getStorage("evaluationIsChild")

    if( evaluationAnswerList.length>0 && userId === evaluationUserId && patientId === evaluationPatientId && evaluationIsChild === 2){
      this.setData({
        showTip: true
      })
    }else{
      isNavigate = 1
      wx.redirectTo({
        url: '/pages/activity/evaluationAnswer/index?isChild=2',
      })
    }
  },
  // 重新评估
  handleReturn(){
    this.setData({
      showTip: false
    })
    app.removeQuestionStorage()
    isNavigate = 1
    wx.redirectTo({
      url: '/pages/activity/evaluationAnswer/index?isChild=2',
    })
  },
  // 继续评估
  handleContinue(){
    this.setData({
      showTip: false
    })
    isNavigate = 1
    wx.redirectTo({
      url: '/pages/activity/evaluationAnswer/index?isChild=2',
    })
  },

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},
	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {
		console.log('isNavigate',isNavigate);
    if(isNavigate === 0){
			wx.reLaunch({
				url: '/pages/index/index',
			})
    }
	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},
})
